﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Drawing;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TICC_IMAGES : ViewBase
    {
        #region ==PHOTOLINK===========
        public static readonly DependencyProperty PHOTOLINKProperty = DependencyProperty.Register("PHOTOLINK",
            typeof(string), typeof(TICC_IMAGES),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPHOTOLINKChanged));
        public string PHOTOLINK
        {
            get
            {
                return (string)GetValue(PHOTOLINKProperty);
            }
            set
            {
                SetValue(PHOTOLINKProperty, value);
                UPLOADIMG();
            }
        }
        private static void OnPHOTOLINKChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TICC_IMAGES;
            if (src != null)
            {
                src.PHOTOLINK = (string)e.NewValue;
            }
        }
        #endregion
     

        #region ==KEY===========
        public static readonly DependencyProperty KEYProperty = DependencyProperty.Register("KEY",
            typeof(string), typeof(TICC_IMAGES),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKEYChanged));
        public string KEY
        {
            get
            {
                return (string)GetValue(KEYProperty);
            }
            set
            {
                SetValue(KEYProperty, value);
                LOADIMG();
            }
        }
        private static void OnKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TICC_IMAGES;
            if (src != null)
            {
                src.KEY = (string)e.NewValue;
            }
        }
        #endregion
        /// <summary>
        /// 选择文件，目录为销售单行，文件名，从服务器上传，下载到本地，先从本地打开，找不到则从服务器上下载
        /// </summary>
        public TICC_IMAGES()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_IMAGES(); 
            this.DataContext = MV;
            InitializeComponent();
            IsEdit = false;
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
            SResult = ((VICC_IMAGES)MV).QueryICC_IMAGES(null, 0);
            MergeData(SResult, false);
            MV.SetProd.Merge(SResult);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

        }
        public void LOADIMG()
        {
            if (KEY.Trim().Length == 0)
                return;
            dwData = MV.SetProd.Tables["ICC_IMAGES"].NewRow();
            dwData["KEY"] = KEY.Trim(); 
            SResult = ((VICC_IMAGES)MV).QueryICC_IMAGES(dwData, 1);
            MergeData(SResult, true);
            /*MemoryStream img = new MemoryStream();
            byte[] blob = (byte[])SResult.Tables[0].Rows[0]["IMAGE"];//
            img.Write(blob, 0, blob.Length);
            System.Drawing.Image image = System.Drawing.Image.FromStream(img);
            imgViewer.Source = image;*/

        }
        public void UPLOADIMG()
        {
            if (PHOTOLINK.Trim().Length == 0)
                return;
            if (KEY.Trim().Length == 0)
                return;
            if (!System.IO.File.Exists(PHOTOLINK))
            {
                return;
            }
            FileStream fs = new FileStream(PHOTOLINK, FileMode.Open, FileAccess.Read);
            byte[] img = new byte[fs.Length];
            fs.Read(img, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            dwData = MV.SetProd.Tables["ICC_IMAGES"].NewRow();
            ((VICC_IMAGES)MV).SetUpNewRow(ref dwData);
            dwData["KEY"] = KEY.Trim();
            dwData["ROWNID"] = INTEXDBC.GetSeq();
            ((VICC_IMAGES)MV).InsertICC_IMAGES(dwData, img, app.company);
        }
    }
}
